<template>
  <el-dialog :title="$t('splitFee.title')" :visible.sync="visible" width="50%">
    <el-form :model="splitFeeInfo" label-width="120px">
      <el-form-item :label="$t('splitFee.timePeriod')">
        <div>{{ splitFeeInfo.endTime }} ~ {{ splitFeeInfo.deadlineTime }}</div>
      </el-form-item>
      <el-form-item :label="$t('splitFee.splitTime')" required>
        <el-date-picker
          v-model="splitFeeInfo.splitTime"
          type="date"
          :placeholder="$t('splitFee.splitTimePlaceholder')"
          value-format="yyyy-MM-dd"
          style="width:100%">
        </el-date-picker>
        <span style="color: red;">{{ $t('splitFee.note') }}</span>
      </el-form-item>
      <el-form-item :label="$t('splitFee.remark')" required>
        <el-input
          type="textarea"
          v-model="splitFeeInfo.remark"
          :placeholder="$t('splitFee.remarkPlaceholder')"
          :rows="3"
          style="width:100%">
        </el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="close">{{ $t('common.cancel') }}</el-button>
      <el-button type="primary" @click="_doSplitFee">{{ $t('common.submit') }}</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { splitFee } from '@/api/fee/listCarFeeApi'

export default {
  name: 'SplitFee',
  data() {
    return {
      visible: false,
      splitFeeInfo: {
        feeId: '',
        splitTime: '',
        remark: '',
        endTime: '',
        deadlineTime: ''
      }
    }
  },
  methods: {
    open(fee) {
      this.splitFeeInfo = {
        feeId: fee.feeId,
        endTime: this.$formatDate(fee.endTime),
        deadlineTime: this._computeSplitDeadLineTime(fee),
        splitTime: '',
        remark: ''
      }
      this.visible = true
    },
    close() {
      this.visible = false
    },
    _doSplitFee() {
      if (!this.splitFeeInfo.splitTime) {
        this.$message.error(this.$t('splitFee.splitTimeRequired'))
        return
      }
      
      const data = {
        preFeeId: this.splitFeeInfo.feeId,
        splitTime: this.splitFeeInfo.splitTime,
        remark: this.splitFeeInfo.remark
      }
      
      splitFee(data).then(response => {
        if (response.code === 0) {
          this.$message.success(this.$t('common.operateSuccess'))
          this.$emit('success')
          this.close()
        } else {
          this.$message.error(response.msg)
        }
      }).catch(error => {
        this.$message.error(error.message)
      })
    },
    _computeSplitDeadLineTime(fee) {
      if (fee.amountOwed == 0 && fee.endTime == fee.deadlineTime) {
        return "-"
      }
      if (fee.state == '2009001') {
        return "-"
      }
      return this.$formatDate(fee.deadlineTime)
    }
  }
}
</script>